clear
set more off

*Pongo juntos primaria general, primaria indígena y secundaria
use "$basein/Datos adicionales/Primaria_general_911.dta"
append using "$basein/Datos adicionales/Primaria_indigena_911.dta"


*Mergeo base de estratos
rename clavecct school_id
merge m:1 school_id using "$basein/Datos adicionales/estratos.dta", gen(_estratos)
drop if estrato==13 | estrato==15


merge m:1 school_id using "$basein/Datos adicionales/secundarias.dta", gen(_secundaria)
drop if secondary==1
drop if _secundaria==2
drop _secundaria

merge m:1 school_id using "$basein/Datos adicionales/estratos.dta", gen(_estrato)
drop if _estrato==2

***Traigo base con variables de interes***
rename school_id cct
merge m:1 cct using "$basein/Datos adicionales/Tratamientos_completos.dta"
drop if _merge==2
drop _merge



***Dejo solo las variables de interes***
*keep if _merge==3


******************
***KEEP´ONLY PEC VS PEC PLUS******
******************
gen ENT=substr(cct,1,2)
destring ENT, replace
keep if ENT==10 | ENT==15 | ENT==17 | ENT==29 | ENT==11 | ENT==27 | ENT==21
drop if state==10
*drop if treatgroup2=="Control"

rename cct clavecct

*** Resalto quienes son participantes y quienes no

gen Participante="No Participa"
replace Participante="Participa" if treatgroup2=="PEC"|treatgroup2=="PEC Plus" /*|treatgroup2=="Control"*/

***** Traigo los datos de PLANEA2018 *****
merge m:1 clavecct using "$base_out/Resultados_planea_2018_nivelescuelaModified.dta", gen(_planea18)


***Estadisticas simples***


table treatgroup2, c(mean T_estu mean T_profe mean PT_ratio)
table treatgroup2 if Nivel=="Primaria", c(mean T_estu mean T_profe mean PT_ratio)

foreach var in T_estu T_profe PT_ratio{
	sum `var'
	matrix tempm=r(mean)
	local tempm=string(tempm[1,1], "%9.2fc")
	file open newfile using "$tables/`var'_mean.tex", write replace
	file write newfile "`tempm'"
	file close newfile
}

save "$base_out/911_All.dta", replace

collapse (first) Nivel (first) treatgroup2 (mean) T_estu (mean) T_profe (mean) PT_ratio (mean) estrato (mean) state (first) marginalization (first) Participante, by(clavecct)
merge 1:m clavecct using "$base_out/Modified_BaseContrasteInteres.dta", keepusing(Urbanas)
duplicates drop

merge 1:m clavecct using "$base_out/Planea2015_Escuelas.dta", gen(_planea15)


**Traigo variable urbanas

collapse (mean) ENT (first) Nivel (first) GRADODEMARGINACIÓN (mean) mathI (mean) mathII (mean) mathIII (mean) mathIV (mean) langI (mean) langII (mean) langIII (mean) langIV (mean) Urbanas (first) treatgroup2 (mean) T_estu (mean) T_profe (mean) PT_ratio (mean) estrato (mean) state (first) marginalization (first) Participante, by(clavecct)								
  
***Regresiones simples***

gen PEC_Plus=0
replace PEC_Plus=1 if treatgroup2=="PEC Plus"

gen PEC=0
replace PEC=1 if treatgroup2=="PEC"

gen Control=0
replace Control=1 if treatgroup2=="Control"
********************************+

gen f_studteach_ratio=T_estu/T_profe
label variable f_studteach_ratio "# of students/# of teachers"
																		
gen Marginalization_=0
replace Marginalization_=1 if GRADODEMARGINACIÓN=="Muy alta o Alta"		

/*
gen participacionppandp=0
replace participacionppandp=1 if treatgroup2=="PEC"|treatgroup2=="PEC Plus" & ENT==15 

gen participacionppandc=0
replace participacionppandc=1 if treatgroup2=="Control"|treatgroup2=="PEC Plus" & ENT==15
*/

gen exp1EdoMex=0 
replace exp1EdoMex=1 if state==9

gen exp2EdoMex=0 
replace exp2EdoMex=1 if state==10

																	
save "$base_out/911_All_Collapsed.dta", replace


